package com.sgb.member.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.sgb.member.R
import com.sgb.member.databinding.RemberMemberItemBinding
import com.sgb.member.entity.RemoveMemberBean
import java.lang.StringBuilder

class RemberMemberAdapter(
    val context: Context,
    var datas: MutableList<RemoveMemberBean>?,
    val calBack: (bean: RemoveMemberBean) -> Unit
) :
    RecyclerView.Adapter<RemberMemberAdapter.ViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding: RemberMemberItemBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.rember_member_item, parent, false
        );
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        datas?.get(position)?.run {
            holder.binding.bean = this
            holder.binding.tvUserType.text = if (relation == "1") "供方会员" else "需方会员"
            allCategoryNames?.let {
                val sb = StringBuilder()
                for (i in allCategoryNames.indices) {
                    if (i != 0) {
                        sb.append("/")
                    }
                    sb.append(allCategoryNames[i])
                }
                val str = sb.toString()
                holder.binding.tvMemberType.text = if (str.isNotEmpty()) str else "/";
            }

            logo?.let {
                if (it.isNotEmpty()) {
                    Glide.with(context).load(it).into(holder.binding.companyPic)
                }
            }

            holder.binding.item.setOnClickListener {
                calBack.invoke(this)
            }
        }

        holder.binding.executePendingBindings()
    }

    override fun getItemCount(): Int = datas?.size ?: 0


    inner class ViewHolder(val binding: RemberMemberItemBinding) :
        RecyclerView.ViewHolder(binding.root)
}

